import Events from "./Events";
import PluginComp from "./PluginComp.vue";
import PluginEvent from "./PluginEvent.vue";
import LoadingComponent from './Loading.vue';
import ErrorComponent from './Error.vue'
import { defineAsyncComponent } from 'vue'

class CornerStone extends Events {
    Router
    PluginEvent = {}
    constructor(InitEvent, router) {
        super();
        this.Router = router;
        this.Vue = InitEvent();

        this.Vue.config.globalProperties.$PluginComp = {};
        this.Vue.config.globalProperties.$PluginEvent = {};

        this.Vue.component('PluginComp', PluginComp);
        this.Vue.component('PluginEvent', PluginEvent);

        this.Vue.provide('Plugin', this.GetPluginEvent);

        var _this = this;
        this.Vue.provide('AppRunEvent', function () {
            _this.run.apply(_this, arguments)
        });
        this.Vue.provide('AppOnEvent', (key, Event) => {
            this.on(key, Event);
        });
        this.Vue.provide('AppOnceEvent', (key, Event) => {
            this.once(key, Event);
        });
        this.Vue.provide('AppOffEvent', (key, Event) => {
            this.off(key, Event);
        });
    }
    GetPluginEvent(key) {
        if (this.Vue.config.globalProperties.$PluginEvent[key]) {
            return this.Vue.config.globalProperties.$PluginEvent[key];
        } else {
            return Empty
        }
    }
    // 注册 插件 的 方法
    RegistPluginEvent(key, event) {
        this.Vue.config.globalProperties.$PluginEvent[key] = event;
    }
    // 注册 插件 的 组件
    RegistPluginCompontent(key, comp) {
        this.Vue.config.globalProperties.$PluginComp[key] = defineAsyncComponent({
            loader: comp,
            loadingComponent: LoadingComponent, /* 在加载时显示 */
            errorComponent: ErrorComponent, /* 显示是否有错误 */
            delay: 0, /* 在显示加载组件之前延迟毫秒 */
            timeout: 3000 /* 这个毫秒之后的超时 */
        });
    }
    // 注册 插件 的路由
    AddRouter(inputRouter) {
        for (let i = 0; i < inputRouter.length; i++) {
            this.Router.addRoute(inputRouter[i]);
        }
    }
    // 组件加载器
    render(Plugins) {
        var PromiseList = [];
        for (let i = 0; i < Plugins.length; i++) {
            const element = Plugins[i];
            PromiseList.push(import(/* @vite-ignore */ element + '?t=' + new Date().getTime()))
        }
        Promise.all(PromiseList).then((res) => {
            for (let i = 0; i < res.length; i++) {
                const item = res[i];
                item.default.install(this);
            };
            this.run('BeforeAppload', this);
            this.Vue.use(this.Router)
            this.Vue.mount('#app');
            this.run('AfterAppload', this);
        })
    }
    // CSS 加载
    renderCss(Plugins) {
        for (let i = 0; i < Plugins.length; i++) {
            const element = Plugins[i];
            const link = document.createElement('link');
            link.href = element.replace('index.es.js', 'style.css');
            link.rel = "stylesheet"
            link.crossOrigin = "anonymous"
            document.head.appendChild(link);
        }
    }
    // 初始化
    init() {
        if (import.meta.env.VITE_PLUGIN) {
            var Plugins = new Function('return ' + import.meta.env.VITE_PLUGIN)();
            this.render(Plugins);
        } else {
            fetch('/Server.json').then(async res => {
                var Plugins = new Function('return ' + await res.text())();
                this.renderCss(Plugins);
                this.render(Plugins);
            })
        }
    }
}

export default CornerStone